import 'package:flutter/material.dart';
import 'package:template/common/app_colors.dart';
import 'package:template/common/assets.dart';
import 'package:template/pages/welcome.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  //计数变量
  final duration = 10;
  int number = 10;

  //倒计时函数
  Future<void> _countdown() async {
    number = duration;
    for (var i = 0; i < duration; i++) {
      await Future.delayed(const Duration(seconds: 1), () {
        if (mounted == true) {
          setState(() {
            number--;
          });
        }
      });
      if (number == 0) {
        print("倒计时结束");
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => const Welcome()),
        );
      }
    }
  }

  @override
  void initState() {
    super.initState();
    _countdown();
  }

  //text
  Text _buildText(String text) {
    return Text(
      text,
      style: const TextStyle(
        fontSize: 19,
        // fontFamily: "Poppins",
        fontWeight: FontWeight.bold,
        color: Colors.white,
        // height: 22 / 19,
      ),
    );
  }

  // logo
  Widget _buildLogo() {
    return Stack(
      children: [
        Container(
          width: 120,
          height: 120,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(120 / 2)),
        ),
        Image.asset(
          AssetsImages.logoPng,
          width: 84,
          height: 80,
        ),
      ],
    );
  }

  //主视图
  Widget _buildView(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          //logo
          _buildLogo(),
          const SizedBox(height: 24),
          //标题
          _buildText("Online Market"),

          const SizedBox(height: 27),
          //计数器
          _buildText(number > 0 ? "$number" : "done"),

          //end
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundSplash,
      body: _buildView(context),
    );
  }
}
